package sjtu.ist.warehouse4ws.dao.cust;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.springframework.orm.hibernate3.HibernateTemplate;

import sjtu.ist.warehouse4ws.dao.BaseDaoHibernate;
import sjtu.ist.warehouse4ws.model.CustDomainmodelattr;

public class CustDomainModelAttributeDaoHibernate extends BaseDaoHibernate<CustDomainmodelattr> implements CustDomainModelAttributeDao{

	@Override
	public List<CustDomainmodelattr> findAllCustDomainModelAttributes(
			String custDomainModelId) {
		// TODO Auto-generated method stub
		return (List<CustDomainmodelattr>)getHibernateTemplate().find("from CustDomainmodelattr d where d.domainModelid='"+custDomainModelId+"'");
	}

	@Override
	public boolean isExist(String id) {
		// TODO Auto-generated method stub
		try
		{
			
			CustDomainmodelattr custDomainmodelattr = (CustDomainmodelattr) getHibernateTemplate()
					.find("from CustDomainmodelattr d where d.id='" + id + "'");
			if (custDomainmodelattr.equals(null)) {
				return false;
			} else {
				return true;
			}

				
		}catch(HibernateException e)
		{
			throw e;
		}
		finally{
		
		}		
	}

	@Override
	public void saveCustDomainmodelattr(CustDomainmodelattr custDomainmodelattr) {
		// TODO Auto-generated method stub
		getHibernateTemplate().setFlushMode(HibernateTemplate.FLUSH_EAGER); 
		getHibernateTemplate().saveOrUpdate(custDomainmodelattr) ;
		
	}

	@Override
	public void createTable(String custName) {
		// TODO Auto-generated method stub
		String sql="create table "+custName+"_cust_domainmodelattr ("
			+"id varchar(100) NOT NULL,"
			+"enName varchar(200) DEFAULT NULL,"
			+"cnName varchar(200) DEFAULT NULL,"
			+"description varchar(500) DEFAULT NULL,"
			+"cnType varchar(200) DEFAULT NULL,"
			+"enType varchar(200) DEFAULT NULL,"
			+"typeId varchar(200) DEFAULT NULL,"
		  	+"minVal varchar(200) DEFAULT NULL,"
		  	+"maxVal varchar(200) DEFAULT NULL,"
		  	+"readOnly tinyint(1) DEFAULT NULL,"
		  	+"staticstate tinyint(1) DEFAULT NULL,"
		  	+"uni tinyint(1) DEFAULT NULL,"
		  	+"collectionType varchar(200) DEFAULT NULL,"
		  	+"defaultValue varchar(200) DEFAULT NULL,"
		  	+"domainModelid varchar(100) NOT NULL,"
		  	+"groupId varchar(100) DEFAULT '',"
		  	+"versionId int(100) DEFAULT NULL,"
		  	+"PRIMARY KEY (id)"
		  	+")";
		SQLQuery query=getSession().createSQLQuery(sql);
		query.executeUpdate();
	}

}
